<template>
	<div class="zh-examination-auth">
		<nav-header></nav-header>
		<div class="zh-table">
			<div class="zh-table-open clear">
				<div class="zh-open-left fl">
					<div class="open-width" style="width: 100px">
						<el-select v-model="study_section_id" filterable placeholder="请选择">
						    <el-option
						      v-for="item in orgLevel2"
						      :key="item.id"
						      :label="item.name"
						      :value="item.id">
						    </el-option>
						</el-select>
					</div>
					<div class="open-width" style="width: 100px">
						<el-select v-model="grade_id" filterable  placeholder="请选择">
						    <el-option
						      v-for="item in orgLevel3"
						      :key="item.id"
						      :label="item.name"
						      :value="item.id">
						    </el-option>
						</el-select>
					</div>
					<div class="open-width" style="width: 100px">
						<el-select v-model="subject_id" filterable  placeholder="请选择">
						    <el-option
						      v-for="item in orgSubject"
						      :key="item.id"
						      :label="item.name"
						      :value="item.id">
						    </el-option>
						</el-select>
					</div>
					<div class="open-width" style="width: 100px">
						<el-select v-model="source_id" filterable placeholder="请选择">
						    <el-option
						      v-for="item in sourceData"
						      :key="item.id"
						      :label="item.name"
						      :value="item.id">
						    </el-option>
						</el-select>
					</div>
					
					<div class="open-width">
						<el-input v-model="keyword" placeholder="请输入试卷名搜索" @keyup.enter.native="resarch"></el-input>
					</div>
					<el-button type="primary" @click="resarch">搜索</el-button>
					<el-button type="primary" @click="reset">重置</el-button>
				</div>
				<div class="zh-open-right fr">
				</div>
			</div>
			<div class="zh-table-contents"
				v-loading="loading.status"
				:element-loading-text="loading.txt"
				>
				<el-table
				    :data="listData"
				    border
				    max-height="600"
				    style="width: 100%">
				    <el-table-column
				      type="index"
                      :index="typeIndex"
				      label="序号"
				      align="center"
				      show-overflow-tooltip
				      width="50"
				      >
				    </el-table-column>
				    <el-table-column
				      prop="name"
				      label="试卷标题"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
				    <el-table-column
				      prop="source"
				      label="类型"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
				    <el-table-column
				      prop="grade"
				      label="年级"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
				    <el-table-column
				      prop="subject"
				      label="学科"
				      align="center"
				      show-overflow-tooltip
				      >
				    </el-table-column>
				    <el-table-column
				      prop=""
				      label="操作"
				      align="center"
				      width="200"
				      show-overflow-tooltip
				      >
				      	<template slot-scope="scope">
			      	        <el-button @click="vivew(scope.row)" type="text">查看</el-button>
			      	        <el-button @click="change_confirm(scope.row , 1)" type="text">批准</el-button>
			      	        <el-button @click="change_confirm(scope.row , 2 )" type="text">拒绝</el-button>
			      	        <el-button @click="delete_confirm(scope.row)" type="text" class="delete-button">删除</el-button>
			      	    </template>
				    </el-table-column>
				</el-table>
				<div class="table-pagination" v-show="total_number > 10">
					<el-pagination
					    @size-change="handleSizeChange"
					    @current-change="handleCurrentChange"
					    :current-page="listParam.page_index"
					    :page-sizes="[10, 20, 30, 40, 50, 100]"
					    :page-size="listParam.page_size"
					    layout="total, sizes, prev, pager, next, jumper"
					    :total="total_number">
					</el-pagination>
					<div class="pagination-button">
						<el-button 
							@click="getList"
							type="primary" icon="el-icon-refresh" 
							circle title="刷新数据" size="mini" >
						</el-button>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
  	export default {
	    data() {
		    return {
		    	loading:{
					status:false,
					txt:'获取数据...',
				},
		       	listData:[],
		    	listParam:{
		    		study_section_id:null,
		    		grade_id:null,
		    		subject_id:null,
		    		version_id:null,
		    		source_id:null,
		       		keyword: '',
		       		page_index:1,
		       		page_size:10,
		       		data_type:2,
		       		status:[3],
		       		authority:[1],
				},
				study_section_id:null,
		    	grade_id:null,
		    	subject_id:null,
		    	version_id:null,
		    	source_id:null,
		       	keyword: '',
		    	orgLevel2Param:{ //获取学段参数
					level:2,
					pid:null,
					direction:'balance',
				},
				orgLevel3Param:{ //获取年纪参数
					level:3,
					pid:null,
					direction:'balance',
				},
				orgLevel2:[{id:null,name:'全部学段'}],
				orgLevel3:[{id:null,name:'全部年级'}],
				orgSubject:[{id:null,name:'全部科目'}],
				subjectParam:{ //获取科目,版本，参数
					type:2,
					org_id:null,
					status:1,
				},
				total_number:0,
				sourceData:[],
				approveStatus:{
		        	1:'批准',
		        	2:'拒绝',
		        }
		    }
		},
		created(){
			this.getSource();
			this.getOrgLevel2();
			this.getList();
		},
		watch:{
			'listParam.study_section_id':{
	    		handler(newVal,oldVal){
					this.listParam.grade_id = null;
					this.listParam.subject_id = null;

					this.orgLevel3Param.pid = newVal;
					this.getOrgLevel3();

					this.subjectParam.org_id = newVal;
					this.getSubject();
					
　　　　　　　　}
			},
			'listParam.grade_id':{
	    		handler(newVal,oldVal){
					this.listParam.subject_id = null;
					this.subjectParam.org_id = newVal;
					this.getSubject();
　　　　　　　　}
			}
		},
		methods: {
			// 试卷查看
			vivew(row){
				this.$router.push("/resources/exam/examinationPaper-view/"+row.id)
			},
			// 序号
            typeIndex(index) {
                return index + (this.listParam.page_index - 1) * this.listParam.page_size + 1;
            },
			getStatus(val){
				return this.statusArr[parseInt(val)]
			},
			getClassType(val){
				return this.classTypeArr[parseInt(val)]
			},
			
			getSource(){
				let success = (data=>{
            		this.sourceData = data.data;
            		this.sourceData.unshift({id:null,name:'全部类型'})
            	})
            	//执行请求，并将回调方法变成config成员以便鉴权过期可以执行
            	this.$ajax.get('api/test/test_source',{params: {},func:{success:success}})
			},
			getList(){
				this.loading.status = true;
            	let success = (data=>{
            		this.loading.status = false;
            		this.listData = data.data.data;
            		this.total_number = data.data.data_sum;
            	})
            	let fail = ( data => {
            		this.loading.status = false;
            		this.$message.error(data.msg)
            	})
            	//执行请求，并将回调方法变成config成员以便鉴权过期可以执行
            	this.$ajax.get('api/test/test',{params: this.listParam,func:{success:success,fail:fail}})
			},
			resarch() {
				this.listParam.study_section_id = this.study_section_id,
				this.listParam.grade_id = this.grade_id,
				this.listParam.subject_id = this.subject_id,
				this.listParam.version_id = this.version_id,
				this.listParam.source_id = this.source_id,
				this.listParam.keyword = this.keyword,
				this.listParam.page_index = 1,
				this.getList();
			},
			reset() {
				this.study_section_id = null;
				this.grade_id = null;
				this.subject_id = null;
				this.source_id = null;
				this.source_id = null;
				this.keyword = '';
				this.orgLevel2Param = { //获取学段参数
					level:2,
					pid:null,
					direction:'balance',
				}
				this.orgLevel3Param = { //获取年纪参数
					level:3,
					pid:null,
					direction:'balance',
				}
				this.subjectParam = { //获取科目,版本，参数
					type:2,
					org_id:null,
					status:1,
				}
				this.listParam = {
					study_section_id:null,
		    		grade_id:null,
		    		subject_id:null,
		    		version_id:null,
		    		source_id:null,
		       		keyword: '',
		       		page_index:1,
		       		page_size:10,
		       		data_type:2,
		       		status:[3],
		       		authority:[1],
				}
				this.getList();
			},
            change_confirm(obj,status){
				let yes = ( data => {
					this.change(obj.id,status);
				})
				let cancel = ( data => {})
				this.e_confirm({yes:yes,cancel:cancel,txt:'确定'+this.approveStatus[parseInt(status)]+' '+obj.name+' 吗？'});

			},
			change(id,status,comment = null){
		    	let success = ( data => {
		    		this.$message.success(data.msg)
					this.getList();
				});
				
		    	this.$ajax.post('api/test/test_approve',{id:id,status:status},{func:{success:success}})
		    },
			//获取学段
			getOrgLevel2(){
				let success = ( data=>{
            		this.orgLevel2 = data.data;
            		this.orgLevel2.unshift({id:null,name:'全部学段'})
            	})
            	this.$ajax.get('api/org/org',{params: this.orgLevel2Param,func:{success:success}})
			},
			//获取年级
			getOrgLevel3(){
				let success = ( data=>{
            		this.orgLevel3 = data.data;
            		this.orgLevel3.unshift({id:null,name:'全部年级'})
            	})
            	this.$ajax.get('api/org/org',{params: this.orgLevel3Param,func:{success:success}})
			},
			//获取科目
			getSubject(){
				let success = ( data=>{
            		this.orgSubject = data.data;
            		this.orgSubject.unshift({id:null,name:'全部科目'});
            	})
            	this.$ajax.get('api/cate/subject',{params: this.subjectParam,func:{success:success}})
			},
			
			delete_confirm(obj){
				let yes = ( data => {
					this.delete(obj.id,0);
				})
				let cancel = ( data => {})
				this.e_confirm({yes:yes,cancel:cancel,txt:'确定将 '+obj.name+' 删除吗？'});

			},
			delete(id,status){
		    	let success = ( data => {
					this.$message.success(data.msg);
					
					if (this.listData.length - 1 === 0) {
						if (this.listParam.page_index > 1) {
							this.listParam.page_index-- ;
						} else {
							this.listParam.page_index = 1;
						}
					}

					this.getList();
				});
		    	this.$ajax.delete('api/test/test',{params:{id:id,status:status},func:{success:success}})
		    },
		    handleSizeChange(val) {
		    	this.listParam.page_index = 1;
		    	this.listParam.page_size = val;
		    	this.getList()
		    },
		    handleCurrentChange(val) {
		    	this.listParam.page_index = val;
		    	this.getList();
		    },
		},
  	}
</script>
<style lang="scss">
	.zh-examination-auth {
		.el-table__body-wrapper table tbody {
			td:nth-child(7) {
				padding: 0;
			}
		}
	}
</style>